﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.BizRule;
using ECCSystem.EntityClass;
using ECC_EXT_DAO.Dao;
using ECC_EXT_Entity;

namespace ECCSystem
{
    public partial class frm_Ext_ParagraghCombine : Form
    {

        public int type { get; set; }
        public EmailTempletEntity objEmailTempletEntity { get; set; }
        private MailParagraghDao _mailParagraghDao;
        private MailParagraghContentDao _mailParagraghContentDao;
        public frm_Ext_ParagraghCombine()
        {
            InitializeComponent();

            _mailParagraghDao = new MailParagraghDao();
            _mailParagraghContentDao = new MailParagraghContentDao();
        }

        private void frm_Ext_ParagraghCombine_Load(object sender, EventArgs e)
        {
            this.LoadParagragh();
            this.LoadParagraghContents();
        }

        private void LoadParagragh(int defaultselected = 0)
        {
            dataGridView1.Rows.Clear();

            IList<MailParagraghEntity> _pgList = 
                this._mailParagraghDao.Search(objEmailTempletEntity.ContentKeyID, this.type);

            foreach (var mailParagraghEntity in _pgList)
            {
                dataGridView1.Rows.Add(mailParagraghEntity.Paragragh,
                                        mailParagraghEntity.Name, 
                                        null, mailParagraghEntity.ID);
            }

            if (defaultselected < _pgList.Count) 
                dataGridView1.Rows[defaultselected].Cells[0].Selected = true;
        }

        private void LoadParagraghContents(string selected = "")
        {
            if(dataGridView1.SelectedCells.Count >0)
            {
                this.comboBox_Contents.Text = "";
                int rowIndex =
                    dataGridView1.SelectedCells[0].RowIndex;
                var paraEntity =
                    this._mailParagraghDao.GetByID(dataGridView1.Rows[rowIndex].Cells[3].Value);

                if(paraEntity!= null)
                {
                    var _list =
                    this._mailParagraghContentDao.Search(paraEntity.ID);
                    //this.comboBox_Contents.Items.Add(new MailParagraghContentEntity());
                    //foreach (var mailParagraghContentEntity in _list)
                    //{
                    //    this.comboBox_Contents.Items.Add(mailParagraghContentEntity);
                    //}
                    
                    this.comboBox_Contents.DataSource = _list;
                    this.comboBox_Contents.DisplayMember = "Name";
                    this.comboBox_Contents.ValueMember = "ID";

                    if (!string.IsNullOrEmpty(selected))
                        this.comboBox_Contents.Text = selected; 

                    if (_list.Count != 0)
                    {
                        var item =
                        this.comboBox_Contents.SelectedItem as MailParagraghContentEntity;

                        this.textBox_Content.Text = item.Content;   

                    }
                    else {
                        this.textBox_Content.Text = "";
                    }

                    this.textBox_ContentsName.Text = "";
                }
                

            }
        }

        private void btn_Paragragh_Add_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(this.textBox_Paragragh_Name.Text))
            {
                MessageBox.Show("Please input name!");
                return;
            }

            MailParagraghEntity _entity = new MailParagraghEntity();
            _entity.EmailContentKeyID = objEmailTempletEntity.ContentKeyID;
            _entity.Name = this.textBox_Paragragh_Name.Text;
            _entity.Type = this.type;
            
            this._mailParagraghDao.Add(_entity);

            this.LoadParagragh(_entity.Paragragh-1);
            this.LoadParagraghContents();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;

            switch (e.ColumnIndex)
            {
                case 0:
                    this.LoadParagraghContents();
                    break;
                case 1:
                    break;

                case 2:
                    this.RevomeParagragh(dataGridView1.Rows[e.RowIndex].Cells[3].Value);

                    break;
            }
        }


        private void RevomeParagragh(object id)
        {
            if (DialogResult.No ==
                    MessageBox.Show("会同时删除段落所属的内容，确定要删除么？", "", MessageBoxButtons.YesNo))
                return;
            this._mailParagraghDao.Remove(id);
            this._mailParagraghDao.ReOrder(objEmailTempletEntity.ContentKeyID, this.type);
            this.LoadParagragh();
            this.LoadParagraghContents();
        }

        private void btn_Up_Click(object sender, EventArgs e)
        {

            int pgIndex;
            int rowIndex = 1;

            if(dataGridView1.SelectedCells.Count >0)
            {
                rowIndex = dataGridView1.SelectedCells[0].RowIndex;

                pgIndex =
                    int.Parse(dataGridView1.Rows[rowIndex].Cells[0].Value.ToString());

                if (pgIndex == 1)
                    return;

                this._mailParagraghDao.ReOrder(objEmailTempletEntity.ContentKeyID, this.type, pgIndex);
            }
            this.LoadParagragh(rowIndex - 1);
        }

        private void btn_Down_Click(object sender, EventArgs e)
        {
            int pgIndex;
            int rowIndex = 1;
            if (dataGridView1.SelectedCells.Count > 0)
            {
                rowIndex = dataGridView1.SelectedCells[0].RowIndex + 1;
                if (rowIndex == dataGridView1.Rows.Count)
                    return;
                pgIndex =
                    int.Parse(dataGridView1.Rows[rowIndex].Cells[0].Value.ToString());

                this._mailParagraghDao.ReOrder(objEmailTempletEntity.ContentKeyID, this.type, pgIndex);
            }

            this.LoadParagragh(rowIndex);
        }

        private void btn_ContentsAdd_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.textBox_ContentsName.Text))
            {
                MessageBox.Show("Please input name!");
                return;
            }


            int rowIndex = 0;
            if(this.dataGridView1.SelectedCells.Count > 0)
            {
                rowIndex =
                    this.dataGridView1.SelectedCells[0].RowIndex;
            }else
            {
                MessageBox.Show("Please create paragragh first!");
                return;
            }
            object ParagraghId = this.dataGridView1.Rows[rowIndex].Cells[3].Value;


            var _ContentEntity = new MailParagraghContentEntity();

            var _ParagraghEntity =
                //this._mailParagraghDao.GetByID(Int64.Parse("9999"));
                this._mailParagraghDao.GetByID(ParagraghId);

            if (_ParagraghEntity == null)
            {
                
                MessageBox.Show("Not found selected paragragh!");
                this.LoadParagragh();
               
                return;
            }


            _ContentEntity.Name = this.textBox_ContentsName.Text;
            _ContentEntity.Content = this.textBox_Content.Text;
            _ContentEntity.ParagraphID = _ParagraghEntity.ID;

            _ContentEntity.UpdateTime = DateTime.Now;
            _ContentEntity.UpdateUser = ClsCommon.gStrOperatorName;

            this._mailParagraghContentDao.SaveOrUpdate(_ContentEntity);

            this.LoadParagraghContents(_ContentEntity.Name);
        }

        private void btn_ContentsDelete_Click(object sender, EventArgs e)
        {
            var item = this.comboBox_Contents.SelectedItem as MailParagraghContentEntity;

            if (item != null)
            {
                if (DialogResult.No == MessageBox.Show("确认删除" + item.Name + "吗？", "Confirm", MessageBoxButtons.YesNo))
                {
                    return;
                }

                this._mailParagraghContentDao.Delete(item);
                this.LoadParagraghContents();
            }
        }

        private void comboBox_Contents_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item =
                this.comboBox_Contents.SelectedItem as MailParagraghContentEntity;
            if (item == null)
            {
                this.textBox_Content.Text = "";
                //this.btn_ContentsAdd.Enabled = true;
                //this.btn_ContetnsModify.Enabled = false;
            }
            else
            {
                this.textBox_Content.Text = item.Content;
                //this.btn_ContentsAdd.Enabled = false;
                //this.btn_ContetnsModify.Enabled = true;
            }
        }

        private void btn_ContetnsModify_Click(object sender, EventArgs e)
        {
            var item =
                this.comboBox_Contents.SelectedItem as MailParagraghContentEntity;
            if(item != null)
            {
                item.Content = this.textBox_Content.Text;

                this._mailParagraghContentDao.SaveOrUpdate(item);
            }

        }

        private void textBox_Content_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            {
                ((TextBox)sender).SelectAll();

            }
        }

    }
}
